package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

@RestController
@RequestMapping("/employee")
@Slf4j
public class EmployeeController {
    @Autowired
    EmployeeService employeeService;

    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee) {
        LambdaQueryWrapper<Employee> qw = new LambdaQueryWrapper<>();
        qw.eq(Employee::getUsername, employee.getUsername());
        Employee one = employeeService.getOne(qw);
        String username = one.getUsername();
        String password = one.getPassword();
        if (username == null)
            return R.error("登陆失败");

        if (!password.equals(DigestUtils.md5DigestAsHex(employee.getPassword().getBytes())))
            return R.error("登陆失败");

        if (one.getStatus() == 0)
            return R.error("用户已被禁用");

        one.setPassword(null);

        request.getSession().setAttribute("employee", one);

        return R.success(one);
    }

    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request) {
        request.getSession().removeAttribute("employee");
        return R.success("退出成功");
    }

    @PostMapping
    public R<String> save(HttpServletRequest request, @RequestBody Employee employee) {
        Employee employee1 = (Employee) request.getSession().getAttribute("employee");
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        //employee.setCreateTime(LocalDateTime.now());
        //employee.setCreateUser(employee1.getId());
        //employee.setUpdateTime(LocalDateTime.now());
        //employee.setUpdateUser(employee1.getId());
        employeeService.save(employee);
        return R.success("保存成功");
    }

    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String name) {
        log.info("page = {},pageSize = {},name = {}", page, pageSize, name);
        LambdaQueryWrapper<Employee> qw = new LambdaQueryWrapper<>();
        qw.like(StringUtils.isNotEmpty(name), Employee::getName, name);
        Page pageInfo = new Page(page, pageSize);

        qw.orderByDesc(Employee::getUpdateTime);
        Page page1 = employeeService.page(pageInfo, qw);

        return R.success(page1);
    }

    @PutMapping
    public R<String> update(HttpServletRequest request, @RequestBody Employee employee) {
        Employee employee1 = (Employee) request.getSession().getAttribute("employee");
        //employee.setUpdateTime(LocalDateTime.now());
       // employee.setUpdateUser(employee1.getId());
        employeeService.updateById(employee);
        return R.success("修改成功");
    }

    @GetMapping("/{id}")
    public R<Employee> getById(HttpServletRequest request, @PathVariable Long id) {
        log.info("根据id查询员工信息...");
        Employee byId = employeeService.getById(id);
        if (byId != null) {
            return R.success(byId);
        }
        return R.error("没有查到对应员工信息");
    }
}
